﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;



public partial class kecheng_new : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtxuefei.Text = "0";
            txtJigou.Text = Session["sJigou"].ToString();
            Session["sTittle"] = "新建课程";
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //string cfenlei1 = DropDownList1.Text;
        string cfenlei2 = DropDownList2.Text;
        //string cfenlei3 = DropDownList3.Text;
        string cname = TextBox1.Text;
        string cjianjie = TextBox2.Text;
        //string cwriter = TextBox3.Text;
        string claoshi = DropDownList5.SelectedValue.ToString();
        string cjigou = txtJigou.Text.ToString();
        string clx = dpllx.SelectedValue.ToString();
        string cxuefei = txtxuefei.Text.ToString().Trim();
        if (cxuefei.Length == 0)
        {
            cxuefei = "0";
        }
        string cfb = DplFB.SelectedValue.ToString();

        if (cfenlei2 == "" | cname == "" | cjianjie == "")
        {
            //在updateUser.aspx.cs中同样命令正确，在这里却出错！？？？？？？？
            //Response.write("请填写课程分类、课程名称和课程简介");
            //returen;
            Label7.Text = "请输入课程信息";
        }
        else
        {
            if (clx == "培训课程" & cxuefei == "0")
            {
                Label7.Text = "培训课程必须输入学费";
            }
            else
            {
                Conn conn = new Conn();
                DateTime dxtsj1 = DateTime.Now;
                string Sql = "select name from kecheng where (fenlei2='"+cfenlei2+"') and (name='"+cname+"') and (laoshi='"+claoshi+"') and (jigou='"+cjigou+"')";
                if(conn.sqlFirst(Sql)!="0")
                {
                    Response.Write("<script language=javascript>alert('课程已存在！');if(opener!=null){opener.location.reload();window.close();}</script>");
                    conn.Dispose();
                    return;
                }
                Sql = "insert into kecheng (fenlei2,name,jianjie,laoshi,jigou,xtsj1,kechenglx,xuefei,fabuzt,writer) values ('" + cfenlei2 +  "','" + cname + "','" + cjianjie + "','" + claoshi + "','" + cjigou + "','" + dxtsj1 + "','" + clx + "'," + cxuefei + ",'" + cfb + "','" + Session["sUsername"].ToString().Trim() + "')";
                if (conn.ExeSql(Sql) == 1)
                {
                    

                    //添加对应的论坛
                    string sql = "insert into bbsconfig (bbsname) values('" + cname + "')";
                    if (conn.ExeSql(sql) == 1)
                    {
                        string topic = conn.sqlFirst("select max(bbs_id) from bbsconfig");
                        //查找课程编号
                        string kechengid = conn.sqlFirst("select id from kecheng where fenlei2='" + cfenlei2 +"' and name='" + cname + "' and xtsj1='" + dxtsj1 + "' and laoshi='" + claoshi + "' and jigou='" + cjigou + "' and kechenglx='" + clx + "'");
                        //写入课程编号对应的topic
                        if (conn.ExeSql("update kecheng set topic = '" + topic + "' where id=" + kechengid) == 1)
                        {
                            //与下文中以及UpdateUser.aspx.cs中完全一样的命令却执行错误！Response.write("添加成功！" + Sql1);
                            Label7.Text = "课程添加成功！";
                        }
                    }

                }
                else
                {
                    Label7.Text = "课程添加失败！";
                }
                conn.Dispose();
            }
        }
    }


    protected void dpllx_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (dpllx.SelectedItem.ToString() == "免费课程")
        {
            txtxuefei.Text = "0";
            txtxuefei.Enabled = false;
        }
        else
        {
            txtxuefei.Enabled = true;
        }
    }

}